using UnityEngine;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

public class Track : MonoBehaviour
{
	#region Properties
	private static int S_ID = 0; // keeps a running tally of successive Track ID values

	private int _ID; // try to get rid of _ID values since there is currently no way to re-use old IDs. May have a workaround by keeping a list of no longer used ID values somewhere?
	public int ID
	{
		get
		{
			return _ID;
		}
	}

    public Vector3 coord { get; set; }
	public Vector3 rot  { get; set; }
	
	public SplineControlPoint startControlPoint{ get; set;}
	public SplineControlPoint endControlPoint{ get; set;}
	
	//private List<Track> neighbors; // a list of neighboring tracks' IDs :: Changed from List<int> to List<Track>
	private HashSet<Track> neighbors;
	
	#endregion

	//MonoBehavior scripts can't have constructors. Instead we do the setup in the Start function
	// and pass extra information in by calling the setters of the properties that need to be set.
    protected virtual void Start()
    {
        _ID = S_ID++;
        //neighbors = new List<Track>();
		neighbors = new HashSet<Track>();
    }

    // neighbors is used to create track chains that are easy to follow
    // changed to (Track n_Track) from (int n_ID)
    public void addNeighbor(Track n_Track)
    {
        neighbors.Add(n_Track);
        //Console.WriteLine("Track[{0}] has added Track[{1}] to its list of neighbors.", m_ID, n_ID);
    }

    // Changed from (int n_ID) to (Track n_Track)
    public bool removeNeighbor(Track n_Track)
    {
        return neighbors.Remove(n_Track); // returns true if the neighbor ID existed and is now removed; false if the ID cannot be found
    }
	
    public int getNumNeighbors()
    {
        return neighbors.Count();
    }
	
    // changed from RetVal int[] to Track[]
    public Track[] getNeighbors()
    {
        return neighbors.ToArray();
    }
	
	public void printInfo(){
		Console.WriteLine("Track -- Coord = {0}\tRot = {1}",coord, rot);
	}
    public void printNeighbors()
    {
        foreach (Track track in neighbors) // changed enumeration type from int to Track
        {
			/* Changing the amount of information displayed by this from just ID number to the Coordinates, Rotation values */
			/* Can TrackType be added as an ENUM? or part of the TrackConstants by chance??  */
            //Console.WriteLine("\t\tNeighbor: Track[{0}]", track.ID);
			
			Console.WriteLine("Neighbor -- Coord = {0}\tRot = {1}",track.coord, track.rot);
		}
    }

    public bool isEqual(Track t_Track)
    {
        try
        {
			// removed t_Track.ID == this.ID check
			
            return (t_Track.coord == this.coord) &&
                   (t_Track.rot == this.rot);
        }
        catch (NullReferenceException)
        {
            return false;
        }
    }
}
